package cn.iocoder.yudao.module.aiposter.utils;

import cn.iocoder.yudao.module.aiposter.config.PosterConfig;
import cn.iocoder.yudao.module.aiposter.config.RemoteConfig;
import cn.iocoder.yudao.module.aiposter.remote.AiClient;
import cn.iocoder.yudao.module.aiposter.remote.AiClientTest;
import cn.iocoder.yudao.module.aiposter.remote.DrawClient;
import cn.iocoder.yudao.module.aiposter.remote.ImgClient;
import com.microsoft.playwright.options.LoadState;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.*;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = PlaywrightUtil.Application.class)
@ActiveProfiles("unit-test")
public class PlaywrightUtil {

    @Resource
    private DrawClient drawClient;


    public static void main(String[] args) {
        System.out.println(Paths.get("/Users/rain/work_space/java_space/ruoyi-vue-pro/poster_img/cover", "aaa.png"));
    }

    @Test
    public void testScreenshot() {
        try (Playwright playwright = Playwright.create()) {
            List<BrowserType> browserTypes = Arrays.asList(
                    playwright.chromium()
//                    playwright.webkit(),
//                    playwright.firefox()
            );
            for (BrowserType browserType : browserTypes) {
                try (Browser browser = browserType.launch()) {
                    BrowserContext context = browser.newContext();
                    Page page = context.newPage();
                    Path path = Paths.get("/Users/rain/work_space/java_space/ruoyi-vue-pro/poster_img/cover", "screenshot-" + browserType.name() + ".png");
//                    page.navigate("https://playwright.dev/");
                    page.navigate("http://localhost:3000/cw_draw?id=55");
//                    page.waitForLoadState(LoadState.LOAD);
//                    page.waitForLoadState(LoadState.NETWORKIDLE);
//                    page.waitForLoadState(LoadState.DOMCONTENTLOADED);
                    Page.WaitForSelectorOptions selectorOptions = new Page.WaitForSelectorOptions();
                    selectorOptions.setStrict(false);
                    selectorOptions.setTimeout(40000);
                    page.waitForFunction("() => document.getElementById('finishLoad') != null");
//                    page.waitForSelector("#finishLoad", selectorOptions);
                    page.locator("#page-design-canvas").screenshot(new Locator.ScreenshotOptions().setPath(path));
                }
            }
        }
    }

    @Test
    public void screenshot() {
        System.out.println(drawClient.coverScreenshot(55L).toString());
    }

    @Import({
            PosterConfig.class,
//            RestTemplateAutoConfiguration.class,
            DrawClient.class,
    })
    public static class Application {
//        /**
//         * 创建 RestTemplate 实例
//         *
//         * @param restTemplateBuilder {@link RestTemplateAutoConfiguration#restTemplateBuilder}
//         */
//        @Bean
//        public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
//            return restTemplateBuilder.build();
//        }
    }
}
